﻿using YunShop_WebAPI.BLL;
using YunShop_WebAPI.Model;
using System.Collections;
using System.Web;
using System.Web.Http;
using System;
using System.IO;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Web.Hosting;
using System.Configuration;
using System.Globalization;

namespace YunShop_WebAPI.API.Controllers
{
    /// <summary>
    /// 商云商城小程序
    /// </summary>
    public class ShopController : ApiController
    {
        /// <summary>
        /// 1.10、wx.login登陆成功之后发送的请求
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("OnLogin")]
        public Result_Model OnLogin(FMWxLogin entity)
        {
            int status = 0;
            string message = "获取成功";
            ArrayList data = ShopCommon.OnUserLogin(entity, ref status, ref message);
            return new Result_Model(status, message, data);
        }

        /// <summary>
        /// 1.11、微信小程序授权拉取手机号码
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("DecryptPhoneNumber")]
        public Result_Model DecryptPhoneNumber(FMDecryptPhoneNumber entity)
        {
            string usercode = CommonFun.ConvertToString(HttpContext.Current.Request["usercode"]);
            string password = CommonFun.ConvertToString(HttpContext.Current.Request["password"]);
            string appid = CommonFun.ConvertToString(HttpContext.Current.Request["appid"]);
            string code = CommonFun.ConvertToString(HttpContext.Current.Request["code"]);
            int status = 0;
            string message = "获取成功";
            ArrayList data = MiniMngCommon.OnUserLogin(appid, code, usercode, password, ref status, ref message);
            return new Result_Model(status, message, data);
        }

        /// <summary>
        /// 1.12、商品分类列表
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodClassList")]
        public Result_Model GetGoodClassList()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            ArrayList data = BusinessCommon.GetGoodClassList(shop_appid);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.13、商品列表
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodsList")]
        public Result_Model GetGoodsList()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string name = CommonFun.ConvertToString(HttpContext.Current.Request["name"]);
            Int64 class_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["class_id"]);
            int page_index = CommonFun.ConvertToInt(HttpContext.Current.Request["page_index"]) - 1;
            int page_size = CommonFun.ConvertToInt(HttpContext.Current.Request["page_size"]);
            ArrayList data = BusinessCommon.GetGoodsList(shop_appid, name, class_id,page_index, page_size, out int page_count);
            return new Result_Model(1, "获取成功", data, data != null ? 1 : 0, page_count);
        }

        /// <summary>
        /// 1.15、商品详情
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetGoodsDetail")]
        public Result_Model GetGoodsDetail()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);            
            Int64 goods_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["goods_id"]);
            Hashtable data = BusinessCommon.GetGoodsDetail(shop_appid, goods_id);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.51、门店资料
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetShopInfo")]
        public Result_Model GetShopInfo()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            Int64 item_id = SystemInfo.GetSystemAppID(shop_appid, SystemAppID.CorpID);
            Hashtable data = MiniMngCommon.GetBaseItemInfo(shop_appid, item_id, 10);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.16、放入购物车
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("SetShoppingCart")]
        public Result_Model SetShoppingCart()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            Int64 goods_id = CommonFun.ConvertToBigInt(HttpContext.Current.Request["goods_id"]);
            decimal qty = CommonFun.ConvertToBigInt(HttpContext.Current.Request["qty"]);
            BusinessCommon businessCommon = new BusinessCommon();
            object data = businessCommon.SetShoppingCart(shop_appid, goods_id, qty);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.17、购物车
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetShoppingCart")]
        public Result_Model GetShoppingCart()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            ArrayList data = BusinessCommon.GetShoppingCart(shop_appid);
            return new Result_Model(1, "获取成功", data);
        }

        /// <summary>
        /// 1.18、生成订单
        /// </summary>
        [HttpPost]
        [ActionName("AddOrderPay")]
        public Result_Model AddOrderPay()
        {
            try
            {
                string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
                Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                StreamReader rd = new StreamReader(xmlStream);
                string json = rd.ReadToEnd();
                Hashtable row = (Hashtable)PluSoft.Utils.JSON.Decode(json);
                int mPayType = CommonFun.ConvertToInt(row["FPayType"]);
                decimal mAmount = CommonFun.ConvertToInt(row["FAmount"]);
                string remark = CommonFun.ConvertToString(row["FRemark"]);
                ArrayList detailed = row["detail"] as ArrayList;
                int mResult = 0;
                string msg = "保存失败";

                Hashtable data = BusinessCommon.AddOrderPay(shop_appid, mAmount, mPayType, remark, detailed, ref mResult, ref msg);
                return new Result_Model(mResult, msg, data);
            }
            catch (Exception ex)
            {
                return new Result_Model(0, ex.Message, null);
            }
        }

        /// <summary>
        /// 1.19、订单列表
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetOrderList")]
        public Result_Model GetOrderList()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            string name = CommonFun.ConvertToString(HttpContext.Current.Request["name"]);
            int page_index = CommonFun.ConvertToInt(HttpContext.Current.Request["page_index"]) - 1;
            int page_size = CommonFun.ConvertToInt(HttpContext.Current.Request["page_size"]);
            string star_date = CommonFun.ConvertToString(HttpContext.Current.Request["star_date"]);
            string end_date = CommonFun.ConvertToString(HttpContext.Current.Request["end_date"]);
            int state_type = CommonFun.ConvertToInt(HttpContext.Current.Request["state_type"]);
            ArrayList data = BusinessCommon.GetOrderList(shop_appid, name, page_index, page_size,
                star_date, end_date, state_type, out int page_count);
            return new Result_Model(1, "获取成功", data, data != null ? 1 : 0, page_count);
        }

        /// <summary>
        /// 1.53、系统信息
        /// </summary>
        [HttpPost, HttpGet]
        [ActionName("GetSystemInfo")]
        public Result_Model GetSystemInfo()
        {
            string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
            int type = CommonFun.ConvertToInt(HttpContext.Current.Request["type"]);
            int mResult = 0;
            string msg = "保存失败";

            Hashtable data = MiniMngCommon.GetSystemInfo(shop_appid, type, ref mResult, ref msg);
            return new Result_Model(mResult, msg, data);
        }

        /// <summary>
        /// 1.18、生成订单
        /// </summary>
        [HttpPost]
        [ActionName("DelShoppingCart")]
        public Result_Model DelShoppingCart()
        {
            try
            {
                string shop_appid = CommonFun.ConvertToString(HttpContext.Current.Request.Headers["shop_appid"]);
                Stream xmlStream = System.Web.HttpContext.Current.Request.InputStream;
                StreamReader rd = new StreamReader(xmlStream);
                string json = rd.ReadToEnd();
                ArrayList detailed = (ArrayList)PluSoft.Utils.JSON.Decode(json);
                string msg = "删除失败";

                BusinessCommon businessCommon = new BusinessCommon();
                int mResult = businessCommon.DelShoppingCart(shop_appid, detailed, ref msg);
                return new Result_Model(mResult, msg, null);
            }
            catch (Exception ex)
            {
                return new Result_Model(0, ex.Message, null);
            }
        }




    }
}
